package com.jason.platform.supervision.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.beans.WorkResp;
import com.jason.platform.supervision.beans.WorkingVisitBean;
import com.jason.platform.supervision.task.QueryWorkDetailTask;
import com.jason.platform.supervision.task.Task;
import com.jason.platform.supervision.task.TaskListener;
import com.jason.platform.supervision.widght.TitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: InterviewDetailActivity
 * @Description: 工作走访详情
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-25 09:47:20
 */
public class InterviewDetailActivity extends BaseActivity implements View.OnClickListener,TaskListener {

    private static final String TAG = InterviewDetailActivity.class.getSimpleName();

    public static final String KEY_WORDING_VISIT_SIGN = "key_wording_visit_sign";

    private TitleBar mTitleBar;

    private MyHandler<InterviewDetailActivity> mHandler = new MyHandler<InterviewDetailActivity>(this);

    private TextView mNameTextView,mTimeTextView,mAddressTextView,mRecordTextView,mManagerTextView,mPhotoTextView;

    private WorkingVisitBean mWorkingVisitBean;
    private String mWorkId;

    private List<String> mPhotos = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_interview_detail);
        super.onCreate(savedInstanceState);
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.bindActivity(this);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mNameTextView = (TextView) findViewById(R.id.interview_detail_name);
        mTimeTextView = (TextView) findViewById(R.id.interview_detail_time);
        mAddressTextView = (TextView) findViewById(R.id.interview_detail_address);
        mRecordTextView = (TextView) findViewById(R.id.interview_detail_record);
        mManagerTextView = (TextView) findViewById(R.id.interview_detail_manager);
        mPhotoTextView = (TextView) findViewById(R.id.interview_detail_photo);
        mPhotoTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPhotos != null && !mPhotos.isEmpty()){
                    //TODO 查看图片
                    Intent mIntent = new Intent(InterviewDetailActivity.this, PreviewPhotoActivity.class);
                    mIntent.putStringArrayListExtra("images", (ArrayList<String>) mPhotos);
                    mIntent.putExtra("position", 0);
                    startActivity(mIntent);
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mWorkingVisitBean = (WorkingVisitBean) getIntent().getSerializableExtra(KEY_WORDING_VISIT_SIGN);
        if (mWorkingVisitBean != null){
            mWorkId = mWorkingVisitBean.getId();
        }
        updateData();
        QueryWorkDetailTask task = new QueryWorkDetailTask(this);
        task.type = Constant.WorkingVisit.QUERY_WORK_DETAIL;
        task.mParams.put("id", mWorkId);
        task.execute();
    }

    private void updateData(){
        if (mWorkingVisitBean == null){
            return;
        }
        mNameTextView.setText(mWorkingVisitBean.getAdjustName());
        mTimeTextView.setText(mWorkingVisitBean.getInterViewTime());
        mAddressTextView.setText(mWorkingVisitBean.getInterViewAddress());
        mRecordTextView.setText(mWorkingVisitBean.getInterViewWqk());
        mManagerTextView.setText(mWorkingVisitBean.getUserName());
        mPhotos = mWorkingVisitBean.getImagesList();
        if (mPhotos != null && !mPhotos.isEmpty()){
            mPhotoTextView.setText("查看图片");
        }else {
            mPhotoTextView.setText("暂无图片！");
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.search_header_search_btn){
            //TODO 调用接口，进行搜索
        }
    }

    @Override
    public void onTaskResult(Task task) {
        Message msg = new Message();
        msg.obj = task;
        mHandler.sendMessage(msg);
    }

    @Override
    void handleMessage(BaseActivity activity, Message msg) {
        Task task = (Task) msg.obj;
        if (task.type == Constant.WorkingVisit.QUERY_WORK_DETAIL) {
            if (task.isSuccess) {
                WorkResp resp = (WorkResp) task.resData;
                mWorkingVisitBean = resp.getValues().get(0);
                updateData();
            }
        }
    }
}
